//
//  HMHomeOriginalView.swift
//  Weibo
//
//  Created by teacher on 2017/6/8.
//  Copyright © 2017年 teacher. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
import YYText

//  原创微博
class HMHomeOriginalView: UIView {
    //  记录原创微博底部约束
    var originalBottomConstraint: Constraint?
    
    //  引用ViewModel
    var statusViewModel: HMStatusViewModel? {
        didSet {
            
            //  判断ViewModel
            guard let currentViewModel = statusViewModel else {
                return
            }
            
            //  代码执行到此，ViewModel一定有值
            //  原创微博子控件赋值
            if let imgUrl = statusViewModel?.status?.user?.profile_image_url {
                //  加载用户头像
                userImageView.sd_setImage(with: URL(string: imgUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            screenNameLabel.text = currentViewModel.status?.user?.screen_name
//            contentLabel.text = currentViewModel.status?.text
            //  设置富文本完成图文混排
            contentLabel.attributedText = currentViewModel.originalAttributedText
            mbrankImageView.image = currentViewModel.mbrankImage
            verifiedTypeImageView.image = currentViewModel.verifiedTypeImage
            sourceLabel.text = currentViewModel.sourceContent
            timeLabel.text = currentViewModel.timeContent
           
            //  卸载之前的约束
            originalBottomConstraint?.deactivate()
            
            //  判断原创微博是配图信息
            if let picUrls = currentViewModel.status?.pic_urls, picUrls.count > 0 {
                //  有原创微博配图
                //  1. 显示配图
                pictureView.isHidden = false
                //  2. 重新创建原创微博的底部约束
                self.snp.makeConstraints({ (make) in
                    originalBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                //  3. 设置配图显示数据
                pictureView.picUrls = picUrls
            } else {
                //  没有配图信息
                //  1. 隐藏配图
                pictureView.isHidden = true
                //  2. 重新创建原创微博的底部约束
                self.snp.makeConstraints({ (make) in
                    originalBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
        }
    }
    //  MARK:   -- 懒加载控件
    //  用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    private lazy var screenNameLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        label.text = "哈哈"
        return label
    }()
    
    //  时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.orange)
        label.text = "刚刚"
        return label
    }()
    
    //  来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.lightGray)
        label.text = "来自: 新浪微博"
        return label
    }()
    //  会员等级图片
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  微博内容
    private lazy var contentLabel: YYLabel = {
        let label = YYLabel()
        //  设置最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HomeCellMargin
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    
    //  配图
    private lazy var pictureView: HMHomePictureView = {
        let collectionView = HMHomePictureView()
        //  设置原创微博配图颜色
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor.white
        
        //  添加子控件
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(mbrankImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //  设置约束
        userImageView.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            //  设置约束优先级
            make.size.equalTo(CGSize(width: 35, height: 35)).priority(.high)
        }
        
        verifiedTypeImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp.trailing)
            make.centerY.equalTo(userImageView.snp.bottom)
        }
        
        screenNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp.trailing).offset(HomeCellMargin)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(userImageView)
            make.leading.equalTo(screenNameLabel)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp.trailing).offset(HomeCellMargin)
        }
        
        mbrankImageView.snp.makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp.trailing).offset(HomeCellMargin)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(userImageView)
            make.trailing.equalTo(self).offset(-10)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            //  设置默认大小
            //  因为配图的顶部有一个约束，底部也有一个约束，并且设置了配图的大小，那么高度重复，可以设置约束优先级，覆盖重复的约束
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }
        
        //  关键约束， 原创微博的底部等于内容的底部加间距10
        self.snp.makeConstraints { (make) in
            //  记录原创微博底部约束
            originalBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        //  设置高亮点击闭包
        contentLabel.highlightTapAction = { (containerView, attributedText, range, rect) in
            //  containerView: 点击的视图contentLabel
            //  attributedText: 点击视图的富文本
            //  range: 点击高亮富文本的范围
            //  rect: 点击高亮的区域
            //  获取高亮的富文本
            let subAttributedText = attributedText.attributedSubstring(from: range)
            //  获取对应高亮富文本的字符串
            let text = subAttributedText.string
            print(text)
            
        }
    }

}
